import http from "../../../utils/https";
const imageUtil = require('../../../utils/images');
// service/pages/leaseHouseDetail/leaseHouseDetail.js
Page({

  /**
   * 页面的初始数据
   */

  data: {
    id:0,   //房屋的id
    residentId:wx.getStorageSync('residentId'),
    houseImage:[],
    houseDetail: {},
    facilities: [
      { name: '冰箱', available: false },
      { name: '洗衣机', available: false },
      { name: '热水器', available: false },
      { name: '床', available: false },
      { name: '衣柜', available: false },
      { name: '管道门', available: false },
      { name: '桌椅', available: false },
      { name: '有线网', available: false },
      { name: '沙发', available: false },
      { name: '沙发', available: false },
      { name: '电视机', available: false },
      { name: '空调', available: false }
    ],
    isFavorite: false
  },


  /**
   * 加载房屋详情
   */
  queryHouseDetail(houseId) {
    // 这里可以调用API获取房屋详情
    console.log('加载房屋详情:', houseId);

    http.get(`houseRental/houseRental/select/${houseId}`)
    .then(resp=>{
      console.log(imageUtil.getImageUrl(resp.data.houseInfo.imageUrl));
        this.setData({
          houseDetail:resp.data, 
          houseImage:imageUtil.getImageUrl(resp.data.houseInfo.imageUrl).split(','),
        })
        resp.data.facilityList.forEach(element => {
          this.data.facilities.forEach(facilitie=>{
            if(element.facilityName==facilitie.name){
              facilitie.available=true
            }
          })
        });
        //重新赋值
        this.setData({
          facilities:this.data.facilities
        })
    })
    .catch(err=>{
      console.log("请求失败",err);
    })
  },

  /**
   * 切换收藏状态
   */
  toggleFavorite() {
    console.log(this.data.isFavorite);
    if(this.data.isFavorite){
      console.log("取消收藏");
      http.delete('collection/collection',{
        houseId:this.data.id,
        residentId:this.data.residentId
      }).then(resp=>{
        this.setData({
          isFavorite: !this.data.isFavorite
        });
      })
      .catch(err=>{
        console.log("请求发送失败",err);
      })

    }else{
      console.log("收藏");
      http.post('collection/collection',{
        houseId:this.data.id,
        residentId:this.data.residentId,

      }).then(resp=>{
        this.setData({
          isFavorite: !this.data.isFavorite
        });
      })
      .catch(err=>{
        console.log("请求发送失败",err);
      })
    }
    // http.post().then(resp=>{

    //   //改变收藏的样式
    // this.setData({
    //     isFavorite: !this.data.isFavorite
    //   });
    // })
    // .catch(err=>{
    //   console.log("请求失败",err);
    // })



  console.log(this.data.isFavorite);
    wx.showToast({
      title: this.data.isFavorite ? '已取消收藏' : '已收藏',
      icon: 'success',
      duration: 1500
    });
  },


  /**
   * 联系房东
   */
  contactLandlord() {
    // 跳转到租房预约页面，传递房屋信息
    wx.navigateTo({
      url: `/service/pages/bookConsultation/bookConsultation?id=${this.data.id}&rentAmount=${this.data.houseDetail.rentAmount}&layout=${this.data.houseDetail.houseInfo.layout}&area=${this.data.houseDetail.houseInfo.usableArea}&phone=${this.data.houseDetail.residentInfo.phoneNumber}&wechat=${this.data.houseDetail.residentInfo.wechatId}`
    });
  },

  /**
   * 租赁房屋
   */
  leaseHouse() {
    // 跳转到租赁页面，传递房屋信息
    wx.navigateTo({
      url: `/service/pages/tenancy/tenancy?id=${this.data.id}&rentAmount=${this.data.houseDetail.rentAmount}&layout=${this.data.houseDetail.houseInfo.layout}&area=${this.data.houseDetail.houseInfo.usableArea}&phone=${this.data.houseDetail.residentInfo.phoneNumber}&landlordName=${this.data.houseDetail.residentInfo.name || '房东'}`
    });
  },

  //判断是否收藏
  isFavorite(){
    console.log("用户id",this.data.residentId);
    console.log("房屋id",this.data.id,);
    //去查询收藏表，如果能查到就说明已经收藏了，如果没查到，就说们没有收藏
    http.get('collection/collection/',{
      houseId:this.data.id,
      residentId:this.data.residentId,
      del:0
    }).then(resp=>{
      console.log("成功获取数据",resp.data);
      if(resp.data !=null || resp.data != undefined){
        this.setData({
          isFavorite:!this.data.isFavorite
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 可以根据传入的房屋ID加载具体房屋信息
    
    if (options.id) {
      this.queryHouseDetail(options.id);
      this.setData({
        id:options.id
      })
    }
    this.isFavorite()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: `${this.data.houseDetail.layout} ¥${this.data.houseDetail.price}/月`,
      path: `/service/pages/leaseHouseDetail/leaseHouseDetail`,
      imageUrl: this.data.houseImages[0]
    };
  }
})